// 箭头函数与this
const person = {
    name:'tom',
    sayHi:function(){
        console.log(`hi,my name is ${this.name}`)
    }
}

person.sayHi()// hi,my name is tom
//======================================================================
const person = {
    name:'tom',
    sayHi:() => {// 箭头函数当中没有this的机制，所以不会改变this的指向，在箭头函数的外面this是什么，那里面拿到的this就是什么
        console.log(`hi,my name is ${this.name}`)
    }
}

person.sayHi() //hi,my name is undefined

const person = {
    name:'tom',
    sayHi:() => {
        console.log(`hi,my name is ${this.name}`)
    },
    sayHiAsync:function(){
        setTimeout(function (){
            console.log(this.name)//函数内部无法拿到当前作用域的this，因为这个函数在setTimeout里面，最终会放在全局对象上被调用，所以无法拿到当前作用域的this对象，拿到的全局对象
        },1000)
    }
}

person.sayHiAsync()
//==============================================================
const person = {
    name:'tom',
    sayHi:() => {
        console.log(`hi,my name is ${this.name}`)
    },
    sayHiAsync:function(){
        const _this = this
        setTimeout(function (){
            console.log(_this.name)//使用闭包的机制拿到当前作用域的this
        },1000)
    }
}

person.sayHiAsync()

const person = {
    name:'tom',
    
    sayHi:() => {
        console.log(`hi,my name is ${this.name}`)
    },
    sayHiAsync:function(){
        setTimeout(() =>{
            console.log(this.name)//有了箭头函数就不用使用闭包机制这么麻烦了，因为箭头函数当中的this始终指向当前作用域里面的this
        },1000)
    }
}

person.sayHiAsync()